package com.telit.android.controller.pub;

import com.telit.android.strategy.context.SmsCodeStrategyContext;
import com.telit.common.entity.DataReturn;
import com.telit.common.util.ValidateCodeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Auther amy
 * @Describe 短信开发端口
 */
@RestController
@RequestMapping("/pub/user")
public class SmsPubController {

    @Autowired
    private SmsCodeStrategyContext smsCodeStrategyContext;

    @PostMapping(value = "/sendSms/{type}", produces = "application/json;charset=UTF-8")
    @ApiOperation("忘记密码/发送验证码,则校验手机号是否存在")
    public DataReturn sendSms(@RequestParam("phone") String phone, @PathVariable("type") String type) throws Exception {
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        return DataReturn.ok(smsCodeStrategyContext.sendSmsCode(type, phone, validateCode.toString()));
    }

    @PostMapping(value = "/checkSmsCode/{type}", produces = "application/json;charset=UTF-8")
    @ApiOperation("校验手机号验证码")
    public DataReturn checkSmsCode(@RequestParam("phone") String phone, @PathVariable("type") String type,
                                   @RequestParam("code") String code, @RequestParam(required = false) Long userId) {
        smsCodeStrategyContext.checkCode(type, phone, code, userId);
        return DataReturn.ok();
    }

}
